package com.example.rabbitmq.demo2_pub_confirm;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.text.resources.CollationData;

import java.util.Date;

/**
 * @Author songyahui
 * @Date 2023/7/12 8:40
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("/pub_confirm")
public class PubConfirmSendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg/{message}")
    public String sendMsg(@PathVariable("message") String message){
        log.info("当前时间：{}发送一条消息{}", new Date(), message);
        //1 关联参数用来做消息的唯一标识，存储在消息的headers中
        CorrelationData correlationData = new CorrelationData();
//        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY, "消息来自发布确认测试："+message, correlationData);
        //2 模拟下异常情况，设置要给不存在的routing key，正常情况下会由回退消息回调方法处理
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, "none_exist", "消息来自发布确认测试："+message, correlationData);
        return "发送成功";
    }
}

